/**
 * Copyright (C) 2013 DaiKit.com - daikit-commons module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/commons
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.commons.shared.utils;

import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Object Utils class
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public abstract class DkObjectUtils
{

	/**
	 * Test if 2 objects are equal.
	 * 
	 * @param obj1
	 * @param obj2
	 * @return a boolean indicating if the 2 given objects are equal or not.
	 */
	@SuppressWarnings("rawtypes")
	public static boolean equalsWithNull(final Object obj1, final Object obj2)
	{
		if (obj1 == null)
		{
			return obj2 == null;
		}
		if (obj1 instanceof Set)
		{
			if (!(obj2 instanceof Set) || !(((Set<?>) obj1).size() == ((Set<?>) obj2).size()))
			{
				return false;
			}
			for (final Object element : (Set<?>) obj1)
			{
				if (!((Set<?>) obj2).contains(element))
				{
					return false;
				}
			}
			return true;
		}
		if (obj1 instanceof List)
		{
			if (!(obj2 instanceof List) || !(((List<?>) obj1).size() == ((List<?>) obj2).size()))
			{
				return false;
			}
			for (int i = 0; i < ((List<?>) obj1).size(); i++)
			{
				if (!equalsWithNull(((List<?>) obj1).get(i), ((List<?>) obj2).get(i)))
				{
					return false;
				}
			}
			return true;
		}
		if (obj1 instanceof Map)
		{
			if (!(obj2 instanceof Map) || !(((Map) obj1).size() == ((Map) obj2).size()))
			{
				return false;
			}
			for (final Object key : ((Map) obj1).keySet())
			{
				if (!equalsWithNull(((Map) obj1).get(key), ((Map) obj2).get(key)))
				{
					return false;
				}
			}
			return true;
		}
		return obj1.equals(obj2);
	}
}
